<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $password
 */
class User extends CDbActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 *
	 * @return user the static model class
	 */
	public static function model($className = __CLASS__)
	{
		return parent::model ( $className );
	}
	protected function beforeSave()
	{
		if (parent::beforeSave ())
		{
			// 自己的行为事件
			if ($this->isNewRecord)
			{
				$this->createtime = $this->updatetime = $_SERVER ['REQUEST_TIME'];
				// $this->author_id=Yii::app()->user->id;
			/**
			 * Adds UUID before the item is saved
			 */
				// $this->id = new CDbExpression ( 'UUID()' );
			} else
			{
				$this->updatetime = $_SERVER ['REQUEST_TIME'];
			}
			return true;
		} else
			return false;
		// return $this->isValid() && parent::beforeSave();
	}
	protected function afterDelete()
	{
		parent::afterDelete ();
		Comment::model ()->deleteAll ( 'post_id=' . $this->id );
		Tag::model ()->updateFrequency ( $this->tags, '' );
	}
	protected function afterFind()
	{
		if ($this->depthid > 0 && $this->depthid == 178)
		{
			$this->depthname = 'aaaaa';
		} else
		{
			$this->depthname = '';
		}
		// echo '<BR><BR>afterFind:';
		$this->cateid = 99;
	}
	protected function cateId($cateid)
	{
		// return $this->cateid;
	}
	/*
	 * protected function getDepthName(){ // echo '<BR>getDepthName';
	 * //'getDepthName'.$this->depthid; return parent::getDepthName(); }
	 */
	
	// protected function setDepthName(){
	// echo '<BR>setDepthName';
	// return 'setDepthName';
	// }
	
	// protected function onCallBack(){}
	
	// protected function onDepthName($id){
	// echo 'onDepthName($id)';
	// }
	/**
	 *
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		//return '{{user}}';
		return 'webapp.wdb_user';
	}
	public function __get($name)
	{
		// parent::__get($name);
		// print_r ( $name );
		// echo '<BR>xx:';
		// var_dump($this->hasAttribute ( $name ));
		// print_r ( $this->$name );
		// parent::__get($name);
		// var_dump($this->hasAttribute ( $name ));
		if ($this->hasAttribute ( $name ) === false)
		{
			// echo '<BR>no hasAttributes:';
			// echo '<BR>__get($name):' . $name;
			// $this->$name .= 'ddd';
			// $this->setAttributes(array($name=>'dddd'), false);
			// var_dump($this->depthname);
			// echo ''
		} else
		{
			// echo '<BR>$name:' . $name;
		}
		if ($name == 'depthname')
		{
			// exit ( 'fdf' );
		}
		return parent::__get ( $name );
	}
	public function __set($name, $value)
	{
		if ($this->hasAttribute ( $name ) === false)
		{
			// echo '<BR>__set:' . $name . ',' . $value;
			
			$this->$name = $value;
		}
		// $this->$name = $value;
		parent::__set ( $name, $value );
		// $this->$name = $value;
	}
	
	/*
	 * public function setAttributes($attributes, $safe = true) {
	 * parent::setAttributes($attributes, $safe); }
	 */
	
	/**
	 *
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array (
				array (
						'username, password','required','className' => 'Users','criteria' => array (
								'condition' => 'active = 1' 
						) 
				),array (
						'username','match','pattern' => '/^[\w\s,]+$/','message' => 'Tags can only contain word characters.' 
				),array (
						'ip','match','pattern' => '/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/' 
				),array (
						'status','in','range' => array (
								1,2,3 
						) 
				),array (
						'createtime','type','type' => 'data','dateFormat' => Yii::app ()->locale->dateFormat 
				),array (
						'createtime, updatetime','unsafe' 
				),array (
						'isactive','boolean' 
				),array (
						'username','length','max' => 255 
				),array (
						'password','length','min'=>6, 'max'=>16
				),
				array('verifypassword', 'compare', 'compareAttribute'=>'password','on' => 'create,changepassword'),
				array (
						'id, username, password','safe','on' => 'search' 
				) 
		);
		/*
		 * $validator = new CNumberValidator;
$validator->attributes = array('id');
$validator->integerOnly = true;
$model->validatorList->add($validator);

相当于array('id', 'numerical', 'integerOnly' => true),

 $rules = array_merge($rules, $this->findDynamicRules()); 
		 */
	}
	
	/**
	 *
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array ();
	}
	
	/**
	 *
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array (
				'id' => 'Id','username' => 'Username','password' => 'Password','depthid' => 'depthid' 
		);
	}
	public function scopes()
	{
		return array (
				// model()->published()->find...()
				'published' => array (
						'condition' => 'status=1' 
				),
				// model()->published()->recently()->find...()
				'recently' => array (
						'order' => 'timestamp DESC','limit' => '6' 
				) 
		);
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 *         based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria = new CDbCriteria ();
		
		$criteria->compare ( 'id', $this->id );
		
		$criteria->compare ( 'username', $this->username, true );
		
		$criteria->compare ( 'password', $this->password, true );
		
		return new CActiveDataProvider ( 'user', array (
				'criteria' => $criteria 
		) );
	}
	
	// 其他代码
	
	/*
	 * public function beforeSave() { parent::beforeSave(); //自己的行为事件 }
	 */
	/*
	public function behaviors()
	{
		return array(
				'crypt' => array(
						// this assumes that the behavior is in the folder: protected/behaviors/
						'class' => 'application.behaviors.CryptBehavior',
						// this sets that the attributes to be encrypted/decrypted are encryptedfieldname of the model
						'attributes' => array('password'),
						'useAESMySql' => true
				)
		);
	}*/
}